
public class Preprocesamiento {

	public Preprocesamiento(){}
	
	public String preprocesarCadena(String cadena){
		//aca se debe: 
		//eliminar espacios en blanco
		//eliminar saltos de linea
		//eliminar caracteres no imprimibles o no pertenecientes al alfabeto (caracteres occidentales, arabigos, etc)
		//eliminar comentarios
		cadena = this.eliminarComentarios(cadena);
		cadena = this.eliminarSaltosLinea(cadena);
		cadena = this.eliminarCaracteresNoImprimibles(cadena);
		cadena = this.eliminarEspaciosBlanco(cadena);

		return cadena;
	}

	private String eliminarComentarios(String cadena) {
		String tmpCadena = "";
		
		while (cadena.indexOf("\n") != -1) {
			if ((cadena.indexOf("#") != -1) && (cadena.indexOf("#") < cadena.indexOf("\n"))) {
				if (!cadena.startsWith("#")) {
					tmpCadena = tmpCadena + cadena.substring(0, cadena.indexOf("#") - 1);
					tmpCadena=tmpCadena + "\n";
				}
			} else {
				tmpCadena = tmpCadena + cadena.substring(0, cadena.indexOf("\n") + 1);
			}
			
			cadena = cadena.substring(cadena.indexOf("\n") + 1, cadena.length());
		}
		
		System.out.println(" ");
		System.out.println("Comentarios Eliminados:");
		System.out.println("=======================");
		System.out.println(tmpCadena);
		
		return tmpCadena;
	}

	private String eliminarCaracteresNoImprimibles(String cadena) {
		char[] chrCadena = cadena.toCharArray();
		
		for (int i = 0; i < chrCadena.length; i++) {
			if(!perteneceAlfabeto(chrCadena[i]))
				chrCadena[i] =' ';
		}
		cadena = new String(chrCadena);
		
		System.out.println(" ");
		System.out.println("Caracteres no Imprimibles Eliminados:");
		System.out.println("=====================================");
		System.out.println(cadena);
		
		return cadena;
	}

	private boolean perteneceAlfabeto(char c) {
		char[] alfabeto = {'{','}','[',']','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','=','!','^','+','-','*','/','.','0','1','2','3','4','5','6','7','8','9', ';','\"','\n','(',')','<','>',':','?',','};
		
		for (int i = 0; i < alfabeto.length; i++) {
			if(Character.toUpperCase(alfabeto[i]) == Character.toUpperCase(c))
				return true;
		}
		return false;
	}

	private String eliminarSaltosLinea(String cadena) {
		while (cadena.indexOf("\n")==0)
			cadena = cadena.substring(1);
		
		while (cadena.contains("\n\n"))
			cadena = cadena.replace("\n\n", "\n");		
		
		System.out.println(" ");
		System.out.println("Saltos de Linea Eliminados:");
		System.out.println("===========================");
		System.out.println(cadena);
		
		return cadena;
	}

	private String eliminarEspaciosBlanco(String cadena) {
		String tmpCadena = "";
		String tmpLinea="";
		String tmpLinea2="";
		char[] analisis = {'{','}','[',']','=','!','^','+','-','*','/','.',';','\"','(',')','<','>',':','?',','};
		
		while (cadena.indexOf("\n") != -1) {
			tmpLinea=cadena.substring(0,cadena.indexOf("\n") + 1);
			
			while (tmpLinea.contains("\"")){
				tmpLinea2 = tmpLinea.substring(0 , tmpLinea.indexOf("\"") + 1);
												
				for (int i = 0; i < analisis.length; i++) {
					tmpLinea2 = tmpLinea2.replace(" " + String.valueOf(analisis[i]),String.valueOf(analisis[i]));
					tmpLinea2 = tmpLinea2.replace(String.valueOf(analisis[i]) + " ",String.valueOf(analisis[i]));	
				}
				
				tmpLinea = tmpLinea.substring(tmpLinea.indexOf("\"") + 1);				
				tmpCadena = tmpCadena + tmpLinea2 + tmpLinea.substring(0 , tmpLinea.indexOf("\"") + 1); 
				tmpLinea = tmpLinea.substring(tmpLinea.indexOf("\"") + 1);			
			}
				
			for (int i = 0; i < analisis.length; i++) {
				tmpLinea = tmpLinea.replace(" " + String.valueOf(analisis[i]),String.valueOf(analisis[i]));
				tmpLinea = tmpLinea.replace(String.valueOf(analisis[i]) + " ",String.valueOf(analisis[i]));	
			}
			
			tmpCadena=tmpCadena + tmpLinea;			
			cadena = cadena.substring(cadena.indexOf("\n") + 1, cadena.length());
		}		
		
		System.out.println(" ");
		System.out.println("Espacios en Blanco Eliminados:");
		System.out.println("===========================");
		System.out.println(tmpCadena);
				
		return tmpCadena;
	}
}
